package cn.cqie.frame.controller;



import cn.cqie.frame.bo.query.QueryEvaluateTargetBo;
import cn.cqie.frame.service.EvaluateTargetService;
import cn.cqie.frame.vo.EvaluateTargetTreeData;
import cn.cqie.frame.vo.EvaluateTargetVo;
import cn.cqie.frame.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 评价指标 前端控制器
 * </p>
 *
 * @author XYHL
 * @since 2021-10-12
 */
@RestController
@Api("评价指标")
public class EvaluateTargetController {
    @Autowired
    private EvaluateTargetService evaluateTargetService;

    /**
     * 新增评价指标
     * @param po
     * @return
     */
    @ApiOperation("新增评价指标")
    @PostMapping(value = "/evaluateTarget/saveEvaluateTarget")
    public ResultVo<EvaluateTargetVo> saveEvaluateTarget(@Valid @RequestBody QueryEvaluateTargetBo po) {
        EvaluateTargetVo evaluateTargetVo = evaluateTargetService.saveEvaluateTarget(po);
        return ResultVo.success(evaluateTargetVo);
    }

    /**
     * 更新评价指标
     * @param po
     * @return
     */
    @ApiOperation("更新评价指标")
    @PostMapping(value = "/evaluateTarget/updateEvaluateTarget")
    public ResultVo<EvaluateTargetVo> updateEvaluateTarget(@RequestBody QueryEvaluateTargetBo po) {
        EvaluateTargetVo evaluateTargetVo = evaluateTargetService.updateEvaluateTarget(po);
        return ResultVo.success(evaluateTargetVo);
    }

    /**
     * 删除评价指标
     * @param EvaluateTargetId
     * @return
     */
    @ApiOperation("删除评价指标")
    @DeleteMapping("/evaluateTarget/deleteEvaluateTarget/{EvaluateTargetId}")
    public ResultVo<Void> deleteEvaluateTarget(@PathVariable Integer EvaluateTargetId) {
        Boolean delete = evaluateTargetService.deleteEvaluateTarget(EvaluateTargetId);
        return delete ? ResultVo.success() : ResultVo.error("删除失败");
    }

    /**
     * 构造elementUi需要的树表数据
     * @return 树形结构的list
     */
    @ApiOperation(value = "评价指标树", notes = "构造elementUi需要的树表数据", response = EvaluateTargetTreeData.class)
    @GetMapping("/evaluateTarget/listTreeEvaluateTarget")
    public ResultVo<List<EvaluateTargetTreeData>> listTreeEvaluateTarget(){
        List<EvaluateTargetTreeData> evaluateTargetTreeData = evaluateTargetService.listTreeEvaluateTarget();
        return ResultVo.success(evaluateTargetTreeData);
    }

    /**
     * 通过id查询对应的上级指标备选项
     * @param id
     * @return
     */
    @ApiOperation(value = "上级指标备选项", notes = "构造elementUi需要的树表数据", response = EvaluateTargetTreeData.class)
    @GetMapping("/evaluateTarget/listParentData")
    public ResultVo<List<EvaluateTargetTreeData>> listParentData(Long id){
        List<EvaluateTargetTreeData> evaluateTargetTreeData = evaluateTargetService.listParentData(id);
        return ResultVo.success(evaluateTargetTreeData);
    }
    /**
     * 批量导入指标
     */
    @ApiOperation(value = "批量导入指标", notes = "导入Excel批量添加")
    @PostMapping(value = "/importExcel")
    public ResultVo<Void> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        Boolean aBoolean = evaluateTargetService.importExcel(file);
        return aBoolean ? ResultVo.success("导入成功") : ResultVo.error("导入失败");

    }
}

